/*
 * Copyright 2019-2025 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * https://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.iiifi.kite.launcher.log;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;

import com.iiifi.kite.launcher.enums.KiteEnv;
import com.iiifi.kite.launcher.service.LauncherService;

/**
 * LogLauncherServiceImpl
 *
 * @author kite@iiifi.com 花朝
 */
public class LogLauncherServiceImpl implements LauncherService {

    @Override
    public void launcher(SpringApplicationBuilder builder, Environment env, String appName, KiteEnv kiteEnv,
            boolean isLocalDev) {
        // 读取系统配置的日志目录，默认为项目下 logs
        String logBase = env.getProperty("LOGGING_PATH", "logs");
        // 用于 spring boot admin 中展示日志
        System.setProperty("logging.file", String.format("%s/%s/info.log", logBase, appName));
        // 配置区分环境的日志
        System.setProperty("logging.config", String.format("classpath:log/log4j2_%s.xml", kiteEnv.getLogFileLevel()));
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }
}
